package org.bt.common.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.bt.common.constant.CommonConstant;
import org.bt.common.enums.ResponseStatus;

import java.io.Serializable;

/**
 * 单体响应信息实体
 *
 * @author tongw
 * @date 2021/7/29
 */
@Data
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 6702346926970880204L;
    @ApiModelProperty(value = "信息元元素")
    private Meta meta;
    @ApiModelProperty(value = "返回对象")
    private T data;

    public Response() {
        this.meta = new Meta().toBuilder().code(ResponseStatus.OK.value()).apiVersion(CommonConstant.ApiVersion.V1).message("请求成功").build();
    }

    public Response(T data) {
        this.meta = new Meta().toBuilder().code(ResponseStatus.OK.value()).apiVersion(CommonConstant.ApiVersion.V1).message("请求成功").build();
        this.data = data;
    }

    public Response(String msg) {
        this.meta = new Meta().toBuilder().code(ResponseStatus.OK.value()).apiVersion(CommonConstant.ApiVersion.V1).message(msg).build();
    }

    public Response(ResponseStatus responseStatus) {
        this.meta =
                new Meta().toBuilder().code(responseStatus.value()).apiVersion(CommonConstant.ApiVersion.V1).message(responseStatus.getReasonPhrase()).build();
    }

    public Response(ResponseStatus responseStatus, String msg) {
        this.meta =
                new Meta().toBuilder().code(responseStatus.value()).apiVersion(CommonConstant.ApiVersion.V1).message(msg).build();
    }

    public Response(String code, String msg) {
        this.meta =
                new Meta().toBuilder().code(code).apiVersion(CommonConstant.ApiVersion.V1).message(msg).build();
    }

    public Response(T data, String msg) {
        this.meta =
                new Meta().toBuilder().code(ResponseStatus.OK.value()).apiVersion(CommonConstant.ApiVersion.V1).message(msg).build();
        this.data = data;
    }

    public Response<T> ok() {
        return new Response<>();
    }

    public Response<T> ok(String msg) {
        return new Response<>(msg);
    }

    public Response<T> code(ResponseStatus responseStatus) {
        return new Response<>(responseStatus);
    }

    public Response<T> code(ResponseStatus responseStatus, String msg) {
        return new Response<>(responseStatus, msg);
    }

    public Response<T> data(T data) {
        return new Response<>(data);
    }


    public Response<T> data(T data, String msg) {
        return new Response<>(data, msg);
    }

    public Response<T> error() {
        return new Response<>(ResponseStatus.INTERNAL_SERVER_ERROR);
    }

    public Response<T> error(String msg) {
        return new Response<>(ResponseStatus.INTERNAL_SERVER_ERROR, msg);
    }
}
